package com.grandtech.insurance.common.bean;

import com.grandtech.insurance.common.utils.collect.Criteria;
import com.grandtech.insurance.common.utils.collect.MultiCriteria;
import com.grandtech.insurance.common.utils.trans.SimpleMap;

import java.io.Serializable;
import java.util.List;

/**
 * Created by zy on 2018/9/15.
 */

public class Page implements Serializable {

    private boolean isPageAvailable = true;
    /**
     * 每页条数
     */
    private Integer pageSize = 30;
    /**
     * 当前页数
     */
    private Integer pageCur = 1;
    /**
     * 排序字段
     */
    private String sort;
    /**
     * 升序还是降序
     */
    private EnumOrder pageOrder;
    /**
     * 查询条件
     */
    private SimpleMap condition;
    /**
     * 查询条件
     */
    private List<Criteria> criteriaList;
    /**
     * 数据库字段
     */
    private Integer limit = pageSize;//记录条数

    private Integer offset = 0;//位置

    private String order = pageOrder == null ? EnumOrder.esc.getName() : pageOrder.getName();//正序倒叙

    public Page() {
    }

    public Page(boolean isPageAvailable) {
        this.isPageAvailable = isPageAvailable;
    }

    public Page(boolean isPageAvailable, Integer pageSize) {
        this.isPageAvailable = isPageAvailable;
        this.pageSize = pageSize;
        this.limit = pageSize;
    }

    public Page setPageAvailable(boolean isPageAvailable) {
        this.isPageAvailable = isPageAvailable;
        return this;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
        this.limit = pageSize;
    }

    public Integer getPageCur() {
        return pageCur;
    }

    public void setPageCur(Integer pageCur) {
        this.pageCur = pageCur;
        offset = getOffset();
    }

    public Page prePage() {
        pageCur = pageCur - 1 >= 1 ? pageCur - 1 : 1;
        return this;
    }

    public Page nextPage() {
        pageCur++;
        return this;
    }

    public int startIndex() {
        return (pageCur - 1) * pageSize;
    }

    public int endIndex() {
        return pageCur * pageSize - 1;
    }

    public int getLimit() {
        return pageSize;
    }

    public int getOffset() {
        return (pageCur - 1) * pageSize;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public EnumOrder getPageOrder() {
        return pageOrder;
    }

    public void setPageOrder(EnumOrder pageOrder) {
        this.pageOrder = pageOrder;
    }

    public SimpleMap addCondition(String fieldName, Object fieldVal) {
        if (condition == null) condition = new SimpleMap();
        condition.push(fieldName, fieldVal);
        return condition;
    }

    public SimpleMap getCondition() {
        if (condition == null) condition = new SimpleMap();
        return condition;
    }

    public void setCondition(SimpleMap condition) {
        this.condition = condition;
    }

    public enum EnumOrder {
        desc("desc"),
        esc("esc");
        String name;

        EnumOrder(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }


    public void setLimit(Integer limit) {
        this.limit = limit;
    }


    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public List<Criteria> getCriteriaList() {
        return criteriaList;
    }

    public void setCriteriaList(List<Criteria> criteriaList) {
        this.criteriaList = criteriaList;
    }

    public MultiCriteria obtainMultiCriteria() {
        return MultiCriteria.create(criteriaList);
    }
}
